package cn.gztang.service.impl;

import cn.gztang.constant.QRCodeInfo;
import cn.gztang.entity.dto.GasTankUpdate;
import cn.gztang.entity.dto.PageQuery;
import cn.gztang.entity.po.GasTankInfo;
import cn.gztang.exception.QRCodeCreateFailException;
import cn.gztang.mapper.GasTankInfoMapper;
import cn.gztang.util.QRCodeUtil;
import cn.gztang.util.ValidateUtil;
import cn.gztang.util.ZipUtil;
import org.dozer.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author GaoFeng2017
 * @date 2018/9/6 15:27
 */
@Service
public class GasTankServiceImpl implements cn.gztang.service.GasTankService {

    @Autowired
    private GasTankInfoMapper gasTankInfoMapper;

    @Autowired
    private Mapper mapper;

    private Logger logger = LoggerFactory.getLogger(GasTankServiceImpl.class);

    @Override
    public int findCountByCodeRange(long begin, long end) {
        return gasTankInfoMapper.selectCountByBarcode(begin, end);
    }

    @Override
    public List<GasTankInfo> findByCodeRange(long begin, long end) {
        return gasTankInfoMapper.selectByCodeRange(begin, end);
    }

    @Override
    public int batchAdd(List<GasTankInfo> gasTankInfos) {
        return gasTankInfoMapper.batchAdd(gasTankInfos);
    }

    @Override
    public List<GasTankInfo> createDefaultGasTankInfoListByRange(long begin, long end) {

        int cap = (int) (end + 1 - begin);

        List<GasTankInfo> infoList = new ArrayList<>(cap);

        for (long i = begin; i <= end; i++) {
            infoList.add(createDefaultGasTankInfo(i));
        }

        return infoList;
    }

    private GasTankInfo createDefaultGasTankInfo(long barcode) {
        GasTankInfo info = new GasTankInfo();
        info.setGmtCreate(new Date());
        info.setCustomerName("");
        info.setDeliveryAddress("");
        info.setFillMedium("");
        info.setFillUsername("");
        info.setFillWeight(0);
        info.setIdCardNumber("");
        info.setDeliveryUsername("");
        info.setTankBarcode(barcode);
        info.setTankStatus("");
        info.setTankType("");
        return info;
    }

    @Override
    public GasTankInfo findByBarcode(long barcode) {
        return gasTankInfoMapper.selectByBarcode(barcode);
    }

    @Override
    public List<GasTankInfo> findAllByPageQuery(PageQuery pageQuery) {
        return gasTankInfoMapper.selectAllByPageQuery(pageQuery.getPageNum(),pageQuery.getPageSize());
    }

    @Override
    public int updateGasTank(GasTankUpdate tankUpdate) {
        GasTankInfo gasTankInfo = mapper.map(tankUpdate, GasTankInfo.class);
        gasTankInfo.setIsUpdate((byte) 1);
        return gasTankInfoMapper.updateByPrimaryKeySelective(gasTankInfo);
    }

    @Override
    public int addGasTankInfo(List<Long> barcodes) {
        List<GasTankInfo> barcodeList = new ArrayList<>(barcodes.size());
        for (long barcode : barcodes) {
            barcodeList.add(createDefaultGasTankInfo(barcode));
        }

        int addCount = 0;
        if (ValidateUtil.checkCollection(barcodeList)) {
           addCount =  gasTankInfoMapper.batchAdd(barcodeList);
        }
        return addCount;
    }

    @Override
    public List<Long> findExitsBarcodeByRange(long begin, long end) {
        return gasTankInfoMapper.selectExitsIdsByRange(begin, end);
    }

    @Override
    public void zipCodeImage(long begin, long end, String parentPath, String realPath, int count) throws QRCodeCreateFailException, IOException {
        int index = 0;
        File[] files = new File[count * 2];
        logger.info("文件数组,{}", Arrays.toString(files));
        for (Long i = begin; i <= end; i++) {

            String s = i.toString();
            String codePath = ZipUtil.generatePath(parentPath, "/" + s);
            String qrcodeFilePath = codePath + QRCodeInfo.QRCODE_FILE_NAME;
            String barcodeFilePath = codePath + QRCodeInfo.BARCODE_FILE_NAME;

            File qrcodeFile = new File(qrcodeFilePath);
            File barcodeFile = new File(barcodeFilePath);

            logger.info("文件,{}", qrcodeFile.getAbsolutePath(), barcodeFile.getAbsolutePath());

            if (!qrcodeFile.exists() || !barcodeFile.exists()) {
                // 创建二维码
                QRCodeUtil.createQRCodeToFile(qrcodeFilePath, QRCodeInfo.CONTENT_PREFIX + s,
                        QRCodeInfo.QRCODE_WIDTH, QRCodeInfo.QRCODE_HEIGHT, QRCodeInfo.DEFAULT_FORMAT);
                // 创建条形码
                QRCodeUtil.createBarcodeToFile(barcodeFilePath, s, QRCodeInfo.BARCODE_WIDTH, QRCodeInfo.BARCODE_HEIGHT, QRCodeInfo.DEFAULT_FORMAT);
            }

            files[index] = qrcodeFile;
            files[index + 1] = barcodeFile;
            index += 2;
        }

        ZipUtil.doCompress(files, realPath);

    }

    @Override
    public GasTankInfo findById(Integer id) {
        return gasTankInfoMapper.selectByPrimaryKey(id);
    }

}
